<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>PostgreSQL 连接</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            max-width: 500px;
            margin: 50px auto;
            background: white;
            padding: 30px;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
        }
        h1 {
            text-align: center;
            color: #333;
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        input {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        button {
            width: 100%;
            padding: 12px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }
        button:hover {
            background-color: #45a049;
        }
        .error {
            color: red;
            margin-top: 5px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>连接PostgreSQL数据库</h1>
        <div class="form-group">
            <label for="host">主机地址</label>
            <input type="text" id="host" placeholder="数据库主机地址" value="localhost" required>
        </div>
        <div class="form-group">
            <label for="port">端口</label>
            <input type="text" id="port" placeholder="数据库运行端口" value="5432" required>
        </div>
        <div class="form-group">
            <label for="username">用户名</label>
            <input type="text" id="username" placeholder="数据库用户名" value="postgres" required>
        </div>
        <div class="form-group">
            <label for="password">密码</label>
            <input type="password" id="password" placeholder="用户密码">
        </div>
        <div class="form-group">
            <label for="database">数据库名称</label>
            <input type="text" id="database" placeholder="数据库名称" value="postgres" required>
        </div>
        <button id="connectBtn">连接</button>
        <div id="connectError" class="error"></div>
    </div>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function() {
            $('#connectBtn').click(function() {
                const host = $('#host').val();
                const port = $('#port').val();
                const username = $('#username').val();
                const password = $('#password').val();
                const database = $('#database').val();

                if (!host || !port || !username || !database) {
                    $('#connectError').text('请填写所有必填字段');
                    return;
                }

                $.ajax({
                    url: '/connect',
                    method: 'POST',
                    data: {
                        host: host,
                        port: port,
                        username: username,
                        password: password,
                        database: database
                    },
                    success: function(response) {
                        if (response.status === 'success') {
                            // 保存连接信息到sessionStorage
                            sessionStorage.setItem('dbConfig', JSON.stringify({
                                host: host,
                                port: port,
                                username: username,
                                password: password,
                                database: database
                            }));
                            window.location.href = '/main';
                        } else {
                            $('#connectError').text(response.message);
                        }
                    },
                    error: function(xhr) {
                        $('#connectError').text('连接失败');
                    }
                });
            });
        });
    </script>
</body>
</html>
